﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using KZ.Exception;
using KZ.Lottery.BLL;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;
using KZ.Utilities;

namespace KZ.Lottery
{
    public partial class FrmReport : Form
    {
        public FrmReport()
        {
            InitializeComponent();
            dtpFrom.CustomFormat = Global.Culture.DateTimeFormat.ShortDatePattern;
            dtpTo.CustomFormat = Global.Culture.DateTimeFormat.ShortDatePattern;
        }

        private void FrmReport_Load(object sender, EventArgs e)
        {
            dtpFrom.Value = DateTime.Today.GetFirstDayOfMonth();
            dtpTo.Value = DateTime.Today.GetLastDayOfMonth();

            LoadRule();
        }

        private void LoadRule()
        {
            try
            {
                RuleList = RuleBLL.GetRuleList();
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadRule", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            dtgRule.EndEdit();
            string id = Global.GetDtgValue(dtgRule, colRuleID.Index, colChk.Index);

            List<RuleReport> ruleReportList = new List<RuleReport>();
            switch (ReportType)
            {
                case "ReportByRule":
                    ruleReportList = RuleReportBLL.GetRuleReportListForRule(id, chkFromStart.Checked, dtpFrom.Value, dtpTo.Value);
                    break;
                case "ReportByChannel":
                    ruleReportList = RuleReportBLL.GetRuleReportListForChannel(id, chkFromStart.Checked, dtpFrom.Value, dtpTo.Value);
                    break;
            }

            if (ruleReportList.Count > 0)
            {
                FrmPreview frm = new FrmPreview();
                frm.ReportType = ReportType;
                frm.RuleReportList = ruleReportList;
                frm.From = dtpFrom.Value;
                frm.To = dtpTo.Value;
                frm.FromStart = chkFromStart.Checked;

                this.Close();

                frm.Show();
            }
            else
            {
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_REPORT_NODATA, false);
            }
        }

        public string ReportType { get; set; }

        public List<Entities.Rule> RuleList
        {
            set
            {
                if (value.Count > 0)
                {
                    dtgRule.Rows.Add(value.Count);
                    for (int i = 0; i < value.Count; i++)
                    {
                        dtgRule[colRuleID.Index, i].Value = value[i].RuleID;
                        dtgRule[colRuleName.Index, i].Value = value[i].RuleName;
                        dtgRule[colDescription.Index, i].Value = value[i].Description;
                    }
                }
            }
        }
    }
}